﻿namespace ExcelTool {
    using System.IO;
    using System.Xml;
    using System.Collections.Generic;
    using System;
    using libxl;
    using System.Xml.Serialization;

    public partial class Converter {
        private SortedDictionary<string, XmlMetalib> m_allLib = new SortedDictionary<string, XmlMetalib>();
        private List<XmlMetalib> m_allCommonLibs = new List<XmlMetalib>();
        private Dictionary<string, Book> m_excel = new Dictionary<string, Book>();
        bool loadAllFinishAssemble(){
            foreach(var lib_it in m_allLib) {
                //收集Excel表格名称
                XmlMetalib lib = lib_it.Value;
                foreach(XmlClassSet cs in lib.class_config) {
                    foreach(var item in cs.DataSourceList) {
                        string fileName = item.tablename;
                        fileName = Path.Combine(fileName); 
                        if(!m_excel.ContainsKey(fileName)) {
                            m_excel.Add(fileName, new XmlBook(fileName));
                        }
                    }
                }
            }
            return true;
        }

        bool QueryCommonDefine(string ref_name) {
            foreach(XmlMetalib lib in m_allCommonLibs) {
                if(lib.ReferenceSets.ContainsKey(ref_name)) return true;
            }

            return false;
        }
        XmlBaseSet GetReferenceOfCommonDefine(string ref_name) {
            XmlBaseSet ret;
            foreach(XmlMetalib lib in m_allCommonLibs) {
                if(lib.ReferenceSets.TryGetValue(ref_name, out ret)) {
                    return ret;
                } 
            }

            return null;
        }
        private bool _parseXmlFile(string fullPath, bool common_use = false) {
            try {

                Console.WriteLine("解析Xml:{0}", fullPath);

                XmlSerializer serializer = new XmlSerializer(typeof(XmlMetalib));
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add(string.Empty, string.Empty);
                FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
                XmlMetalib mt = serializer.Deserialize(fs) as XmlMetalib;


                if(m_allLib.ContainsKey(mt.lib_name)) {
                    Reporter.PushError("metaLib 命名重复{0}", mt.lib_name);
                    return false;
                }

                if(!mt.CheckAndAssemble(common_use)) return false;
                if(common_use) {
                    m_allCommonLibs.Add(mt);
                }
                else {
                    m_allLib.Add(mt.lib_name, mt);
                }

            }
            catch(System.Exception e) {
                Reporter.PushError("载入XML文件异常: {0}", e.ToString());
                return false; 
            }
            return true; 
        }

        public bool LoadXmlFiles() {
            try {
                DirectoryInfo dir = new DirectoryInfo(FILE_PATH.XML_CONFIG_DIR);

                //先载入共有定义
                FileInfo[] commonFI = dir.GetFiles(FILE_PATH.XML_COMMON_DEFINE);
                if(commonFI.Length > 0) {
                    string full_path = Path.Combine(FILE_PATH.XML_CONFIG_DIR, commonFI[0].Name);
                    if(!_parseXmlFile(full_path, true)) return false;
                }

                FileInfo[] fileInfo = dir.GetFiles("*.xml");
                foreach(FileInfo item in fileInfo) {
                    if(string.Compare(FILE_PATH.XML_COMMON_DEFINE, item.Name) == 0) {
                        continue;
                    }
                    string full_path = Path.Combine(FILE_PATH.XML_CONFIG_DIR, item.Name);

                    if(!_parseXmlFile(full_path)) return false;
                }

                //载入完毕做引用检查和数据收集
                if(!loadAllFinishAssemble()) return false;
            }
            catch(System.Exception e) {
                Reporter.PushError("获取XML配置目录失败: {0}", e.ToString());
                return false;
            }

            return true;
        }
    }
}
